<template>
  <div class="details leaderBoards">
    <div class="leaderBoards-item">
      <div class="leaderBoards-item-title">
        <span class="slot">|</span><span class="title">官方榜</span>
      </div>
      <div class="leaderBoards-item-list" v-for="item, index in leaderBoardsItem" @click="goToList(item)" v-show="show">
        <div class="left-img">
          <img :src="item.coverImgUrl" alt="">
        </div>
        <ul>
          <li>1.{{item.tracks[0].name}} - <span v-for="singer, index in item.tracks[0].ar"><span v-if="index>0">/</span>{{singer.name}}</span></li>
          <li>2.{{item.tracks[1].name}} - <span v-for="singer, index in item.tracks[1].ar"><span v-if="index>0">/</span>{{singer.name}}</span></li>
          <li>3.{{item.tracks[2].name}} - <span v-for="singer, index in item.tracks[2].ar"><span v-if="index>0">/</span>{{singer.name}}</span></li>
        </ul>
      </div>
      <loading v-show="!show" class="load"></loading>
    </div>
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
  import Loading from '../../base/loading/loading.vue'
  const ERROk = 200
  const SOAR = 3
  const NEW = 0
  const ORIGINAL = 2
  const HOT = 1
  export default{
    data() {
      return {
        leaderBoardsItem: [],
        show: false
      }
    },
    activated() {
      let leaderBoardsItemIdx = [SOAR, NEW, ORIGINAL, HOT]
      let that = this
      if (this.leaderBoardsItem.length === 0) {
        leaderBoardsItemIdx.forEach(function (v, index) {
          that.getLeaderBoardsList(v, index)
        })
      }
    },
    methods: {
      getLeaderBoardsList(v, index) {
        this.$ajax.get(`/top/list?idx=${v}`).then((response) => {
          if (response.data.code === ERROk) {
            response.data.playlist.itemIdx = v
            response.data.playlist.sortIndex = index
            this.leaderBoardsItem.push(response.data.playlist)
            if (this.leaderBoardsItem.length === 4) {
              this.leaderBoardsItem.sort(this.compare('sortIndex'))
              this.show = true
            }
          }
        })
      },
      compare(property) {
        return function(a, b) {
          var value1 = a[property]
          var value2 = b[property]
          return value1 - value2
        }
      },
      goToList(item) {
        console.log(item)
        this.$router.push({
          path: `/leaderBoards/${item.itemIdx}`
        })
      }
    },
    components: {
      Loading
    }
  }
</script>

<style>
  .details .slot {
    color: #ff4081;
    padding-right: 1rem;
  }

  .leaderBoards .title {
    font-size: 1.5rem;
    font-weight: 800;
  }

  .leaderBoards-item-list .left-img {
    width: 30%;
  }

  .leaderBoards-item-title {
    margin-bottom: 0.8rem;
  }

  .leaderBoards-item .left-img img {
    width: 100%;
  }

  .leaderBoards-item-list {
    position: relative;
    border-bottom: 1px solid #ddd;
    margin-bottom: 0.5rem;
  }

  .leaderBoards-item-list ul {
    position: absolute;
    top: 50%;
    left: 30%;
    transform: translate(0, -50%);
    padding-left: 1rem;
  }

  .leaderBoards-item-list li {
    line-height: 3rem;
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
  }
  .leaderBoards .load{
    margin-top: 5rem;
  }
</style>
